<?php


class TypeAction extends PublicAction
{
    protected $db;
    protected $typeModel;

    function _initialize()
    {
        parent::_initialize();
        $this->typeModel = D('Type');
        $this->Type = getCache('Type');
    }

    public function index()
    {
        //类型列表
        $list = $this->typeModel->where('parentid=0')->select();
        $this->assign('list', $list);

        $keyid = I('get.keyid', 1, 'intval');
        $this->assign('keyid', $keyid);

        //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%' . $keyword . '%');
        $map['typeid|name'] = array($condition, $condition, '_multi' => true); //搜索条件

        //获取所有菜单
        $map['status'] = array('egt', '0'); //禁用和正常状态
        $map['keyid'] = $keyid;
//        $map['parentid'] = array('neq', '0');

        $data_list = $this->typeModel->where($map)->order('listorder asc, typeid asc')->select();

        import('@.ORG.Tree');
        $tree = new Tree();
        $data_list = $tree->toFormatTree($data_list, 'name', 'typeid', 'parentid');

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->assign('type_list', $data_list);

        $this->display();
    }

    function add()
    {
        if (IS_POST) {
            $_POST['status'] = 1;

            if (false === $this->typeModel->create()) {
                $this->error($this->typeModel->getError());
            }

            $typeid = $this->typeModel->add();

            if ($typeid) {

                if (empty($_POST['keyid'])) {
                    $data['typeid'] = $data['keyid'] = $typeid;
                    $this->typeModel->save($data);
                }

                saveCache('Type');

                $this->success('添加成功！', cookie('__forward__'));

            } else {
                $this->error('提交失败！: ' . $this->typeModel->getDbError());
            }
            exit;
        }

        $parentid = intval($_GET['parentid']);
        $keyid = intval($_GET['keyid']);
        $this->assign('keyid', $keyid);
        $array = array();

        if ($parentid) {

            foreach ((array)$this->Type as $key => $r) {
                if ($r['keyid'] != $keyid || empty($r['status'])) continue;
                $r['id'] = $r['typeid'];
                $array[] = $r;
            }

            import('@.ORG.Tree');
            $tree = new Tree($array);
            $select_type = $tree->get_tree(0, $parentid, 'name');
            $this->assign('select_type', $select_type);
        }
        $this->display();
    }


    function edit()
    {
        if (IS_POST) {
            if (false === $this->typeModel->create()) {
                $this->error($this->typeModel->getError());
            }

            if (false !== $this->typeModel->save()) {

                saveCache('Type');

                $this->success('修改成功!', cookie('__forward__'));
            } else {
                $this->success('修改失败!: ' . $this->typeModel->getDbError());
            }
        } else {
            $typeid = intval($_GET['typeid']);
            $parentid = $this->Type[$typeid]['parentid'];
            $keyid = intval($_GET['keyid']);
            $this->assign('keyid', $keyid);
            $array = array();
            if ($parentid) {

                foreach ((array)$this->Type as $key => $r) {
                    if ($r['keyid'] != $keyid) continue;
                    $r['id'] = $r['typeid'];
                    $array[] = $r;
                }

                import('@.ORG.Tree');

                $tree = new Tree($array);
                $tree->nbsp = '&nbsp;&nbsp;';

                $select_type = $tree->get_tree(0, $parentid, 'name');
                $this->assign('select_type', $select_type);
            }


            $pk = ucfirst($this->typeModel->getPk());
            $id = $_REQUEST[$this->typeModel->getPk()];

            if (empty($id))
                $this->error('缺少必要的参数!');

            $do = 'getBy' . $pk;
            $vo = $this->typeModel->$do($id);

            $this->assign('vo', $vo);
            $this->display();
        }
    }

    function delete()
    {
        $typeid = I('typeid');
        if (!$typeid) {
            $this->error('参数不正确！');
        }

        if (false !== $this->typeModel->delete($typeid)) {
            $this->success('删除失败！');
        } else {
            $this->error('删除失败！' );
        }
    }

    public function get_child($linkageid)
    {
        $where = array('parentid' => $linkageid);
        $this->childnode[] = intval($linkageid);
        $result = $this->typeModel->select($where);

        if ($result) {
            foreach ($result as $r) {
                $this->_get_childnode($r['linkageid']);
            }
        }
    }

    public function get_arrparentids($pid, $array = array(), $arrparentid = '')
    {
        if (!is_array($array) || !isset($array[$pid])) return $pid;

        $parentid = $array[$pid]['parentid'];
        $arrparentid = $arrparentid ? $parentid . ',' . $arrparentid : $parentid;
        if ($parentid) {
            $arrparentid = $this->get_arrparentids($parentid, $array, $arrparentid);
        } else {
            $data = array();
            $data['bid'] = $pid;
            $data['arrparentid'] = $arrparentid;
        }
        return $data;
    }

    public function get_arrchildid($id, $array = array())
    {
        $arrchildid = $id;

        foreach ($array as $catid => $cat) {
            if ($cat['parentid'] && $id != $catid) {
                $arrparentids = explode(',', $cat['arrparentid']);
                if (in_array($id, $arrparentids)) $arrchildid .= ',' . $catid;
            }
        }
        return $arrchildid;
    }

    public function listorder()
    {
        $ids = $_POST['listorders'];

        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $this->typeModel->where('typeid=' . $key)->save($data);
        }

        $this->success('提交成功!');
    }
}